//
//  composeTextView.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/3/2.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit

class ComposeTextView: UITextView {
    //MARK ----属性
    //当外本的文本发生变化，那么要接受这种变化，调试以达到最佳的效果
    //当外部的需求改变参数时，也要设置，考虑这些问题
    //当文本发生变化时
    
    var maxlength:Int = 10{
     
        didSet{
        
           tipLabel.text = maxlength.description
        }
    
    }
    
    var placehoderLabel:String? {
        didSet{
        
          placehoder.text = placehoderLabel
        }
    
    }
    //字体变化
    override var font: UIFont? {
    
        didSet{
           placehoder.font = font
        }
    }
    
    override var text: String! {
    
        didSet{
            
        placehoder.hidden = hasText()
        
        }
    }
 
    //MARK---构造函数
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        //监听文本发生变化
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"textChanged", name:UITextViewTextDidChangeNotification, object:nil)
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    @objc private func textChanged(){
        placehoder.hidden = hasText()
    
        
        
        let len = maxlength  - text.characters.count
        
        tipLabel.text = len.description
        
        tipLabel.textColor = (len >= 0) ? UIColor.lightGrayColor() : UIColor.redColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件 
    private lazy var placehoder = UILabel(cztitle:"发现点新鲜事...", color:UIColor.lightGrayColor(), fontSize:18)
    private lazy var tipLabel = UILabel(cztitle:"10", color:UIColor.lightGrayColor(), fontSize:12)
}


extension ComposeTextView{

    private func setupUI(){
        
       // text = "发现点新鲜事..." //测试的
        font = UIFont.systemFontOfSize(18)
        textColor = UIColor.lightGrayColor()
        
        //滚动方向
        alwaysBounceVertical = true
        //屏幕滚动时，退出键盘
        keyboardDismissMode = .OnDrag
        
        //1.添加占位文本
        addSubview(placehoder)
        
        //自动布局
        let margin:CGFloat = 5
        placehoder.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(margin)
            make.top.equalTo(self).offset(8)
           //textView是scrollView的子视图 必须指定其宽度
            make.width.equalTo(self).offset(-margin)
        }
        
        //添加一个view
        let sizeView = UIView()
        
        insertSubview(sizeView, atIndex: 0) //0 代表插入到最底部的视图
        
        sizeView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self) //利用SB 指定布局，相当于四周的约束
            make.size.equalTo(self)
        }
        
        //2.添加长度提示标签
        
        addSubview(tipLabel)
        //这个布局是添加在scrollView上的，必须要添加一个View当做容器
        //不然添加约束的话，是不能找到这个标签的
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self).offset(-margin)
            make.right.equalTo(self).offset(-margin)
        }
    }

}